package cn.dujc.superkid.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.utils.Preferences;

public class ResetPwdActivity extends BaseActivity  {
    private final String TAG = ResetPwdActivity.class.getName();
    private EditText oldPwd;
    private Button resetPwd;
    private EditText newPwd;
    private EditText surePwd;
    private RegisterResult registerResult;
    @Override
    public int getViewId() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        setTitle("修改密码");
        initView();
    }
    private void  initView(){
        resetPwd = findViewById(R.id.resetSure);
        newPwd = findViewById(R.id.newPwd);
        oldPwd= findViewById(R.id.oldPwd);
        surePwd = findViewById(R.id.pwdSure);



            resetPwd.setOnClickListener(new View.OnClickListener() {

                final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);

                public void onClick(View v) {
                    if (newPwd.getText().toString().equals(surePwd.getText().toString())) {
                        if (oldPwd.getText().toString().equals(surePwd.getText().toString())) {
                            Log.i(TAG, "==========caizhijia=logincode============" + loginResult.getLogincode());
                            Log.i(TAG, "==========caizhijia=newpassword============" + surePwd.getText().toString());
                            Log.i(TAG, "==========caizhijia=oldpassword============" + oldPwd.getText().toString());
                            Log.i(TAG, "==========caizhijia=user_id============" + Integer.parseInt(loginResult.getUid()));
                            HttpHelper.getUserCenter().memberChangePassword(QMap.build()
                                    .put("logincode", loginResult.getLogincode())
                                    .put("password_new", surePwd.getText().toString())
                                    .put("password", oldPwd.getText().toString())
                                    .put("user_id", Integer.parseInt(loginResult.getUid())).create()).enqueue(new ResultCallBack<Result<Object>>(mActivity) {
                                @Override
                                public void onSuccessCallback(int code, Result<Object> body) {
                                    if (body != null) {
                                        ToastUtil.showToast(mActivity, body.getMsg());
                                        Preferences.getInstance().saveLoginResult(mActivity, registerResult);
                                        setResult(RESULT_OK);
                                        Intent logoutIntent = new Intent(ResetPwdActivity.this, LoginActivity.class);
                                        startActivity(logoutIntent);
                                        finish();
                                    }
                                }
                            });
                        }
                        else {
                            ToastUtil.showToast(mActivity,"新旧密码一样请重新输入" );
                            newPwd.setText("");
                            surePwd.setText("");
                            oldPwd.setText("");

                        }
                    }
                    else {
                        ToastUtil.showToast(mActivity,"新密码输入不一致请重新输入" );
                        newPwd.setText("");
                        surePwd.setText("");

                    }
                }

            });


    }


}
